<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PayRefundLog extends Model
{
    protected $table = "c_pay_refund_logs";

//    public function order()
//    {
//        return $this->hasOne('App\Models\Order', 'order_id');
//    }

    public function user()
    {
        return $this->hasOne('App\User', 'id','user_id');
    }

    public function work()
    {
        return $this->hasOne('App\Models\DoctorWorks', 'user_id');
    }

//    public function ordergoods()
//    {
//        return $this->hasOne('App\Models\OrderGoods', 'order_id', 'order_id');
//    }

    /**
     * @param $data
     * @param $orderinfo
     * @param $type
     * 微信支付或退款记录
     */
    public static function addWxPayRefundLog($data, $orderinfo, $type, $isrefund = 0)
    {
        $Pay = new PayRefundLog();
        if ($isrefund == 1) { //退款
            $logs = self::where([['transaction_id','=',$data['transaction_id']],['make_type','=',2]])->first();
            if($logs){
                return true;
            }
            $Pay->transaction_id = $data['transaction_id'];
            $Pay->out_trade_no = $data['out_trade_no'];
            $Pay->refund_id = isset($data['refund_id'])?$data['refund_id']:0;
            $Pay->out_refund_no = isset($data['out_refund_no'])?$data['out_refund_no']:0;
            $Pay->order_id = $orderinfo->id;
            if($orderinfo->order_type == 1){
                //商品类型(1:课程,2:商品,3:图文质询,4:电话预约,5:打赏)
                $Pay->order_type =1; //订单类型(1:课程,2:商品,3:质询,4商圈,5:打赏)
            }elseif ($orderinfo->order_type == 2){
                $Pay->order_type =2;
            }elseif ($orderinfo->order_type == 3 || $orderinfo->order_type == 4){
                $Pay->order_type =3;
            }elseif ($orderinfo->order_type == 5){
                $Pay->order_type =5;
            }elseif ($orderinfo->order_type == 6){ //打赏退款
                $Pay->order_type =4;
            }
            $Pay->pay_type = 2;
            $Pay->make_type = 2;
            $Pay->user_id = $orderinfo->user_id;
            $Pay->work_id =isset($orderinfo->work_id)?$orderinfo->work_id:0;
            $Pay->money = sprintf("%.2f",($data['money'] / 100));//$orderinfo->total_price;
            return $Pay->save();
        } else { //付款
            $logs = self::where([['transaction_id','=',$data['transaction_id']],['make_type','=',1]])->first();
            if($logs){
                return true;
            }
            if ($type == 1) { //商圈支付
                $Pay->transaction_id = $data['transaction_id'];
                $Pay->out_trade_no = $data['out_trade_no'];
                $Pay->order_id = $orderinfo->id;
                $Pay->order_type =4;
                $Pay->pay_type = 2;
                $Pay->make_type = 1;
                $Pay->user_id = $orderinfo->user_id;
                $Pay->work_id = 0;
                $Pay->money =  sprintf("%.2f",($data['amount']['total'] / 100));  //$orderinfo->money;
                return $Pay->save();
            } elseif ($type == 2) { //订单支付
                $Pay->transaction_id = $data['transaction_id'];
                $Pay->out_trade_no = $data['out_trade_no'];
                $Pay->order_id = $orderinfo->id;
                if($orderinfo->order_type == 1){  //商品类型(1:课程,2:商品,3:图文质询,4:电话预约,5:打赏)
                    $Pay->order_type =1; //订单类型(1:课程,2:商品,3:质询,4商圈,5:打赏)
                }elseif ($orderinfo->order_type == 2){
                    $Pay->order_type =2;
                }elseif ($orderinfo->order_type == 3 || $orderinfo->order_type == 4){
                    $Pay->order_type =3;
                }elseif ($orderinfo->order_type == 5){
                    $Pay->order_type =5;
                }
                $Pay->pay_type = 2;
                $Pay->make_type = 1;
                $Pay->user_id = $orderinfo->user_id;
                $Pay->work_id =$orderinfo->work_id;
                $Pay->money =  sprintf("%.2f",($data['amount']['total'] / 100)); //$orderinfo->total_price;
                return $Pay->save();
            }
        }
    }

    /***
     * @param $data
     * @param $orderinfo
     * @param $type
     * @param int $isrefund
     * @return bool
     * 支付宝支付退款记录
     */
    public static function addAliPayRefundLog($data, $orderinfo, $type, $isrefund = 0)
    {
        $Pay = new PayRefundLog();
        if ($isrefund == 1) { //退款
            $logs = self::where([['transaction_id', '=', $data['trade_no']], ['make_type', '=', 2]])->first();
            if ($logs) {
                return true;
            }
            $Pay->transaction_id = $data['trade_no'];
            $Pay->out_trade_no = $data['out_trade_no'];
            $Pay->refund_id = isset($data['refund_id'])?$data['refund_id']:0;
            $Pay->out_refund_no = isset($data['out_refund_no'])?$data['out_refund_no']:0;
            $Pay->order_id = $orderinfo->id;
            if ($orderinfo->order_type == 1) {
                //商品类型(1:课程,2:商品,3:图文质询,4:电话预约,5:打赏)
                $Pay->order_type = 1; //订单类型(1:课程,2:商品,3:质询,4商圈,5:打赏)
            } elseif ($orderinfo->order_type == 2) {
                $Pay->order_type = 2;
            } elseif ($orderinfo->order_type == 3 || $orderinfo->order_type == 4) {
                $Pay->order_type = 3;
            } elseif ($orderinfo->order_type == 5) {
                $Pay->order_type = 5;
            }elseif ($orderinfo->order_type == 6) { //商圈驳回退款
                $Pay->order_type = 4;
            }
            $Pay->pay_type = 1;
            $Pay->make_type = 2;
            $Pay->user_id = $orderinfo->user_id;
            $Pay->work_id = isset($orderinfo->work_id)?$orderinfo->work_id:0;
            $Pay->money = $data['total_amount'];//$orderinfo->total_price;
            return $Pay->save();
        } else { //付款
            $logs = self::where([['transaction_id', '=', $data['transaction_id']], ['make_type', '=', 1]])->first();
            if ($logs) {
                return true;
            }
            if ($type == 1) { //商圈支付
                $Pay->transaction_id = $data['trade_no'];
                $Pay->out_trade_no = $data['out_trade_no'];
                $Pay->order_id = $orderinfo->id;
                $Pay->order_type = 4;
                $Pay->pay_type = 1;
                $Pay->make_type = 1;
                $Pay->user_id = $orderinfo->user_id;
                $Pay->work_id = 0;
                $Pay->money = $data['total_amount'];//$orderinfo->money;
                return $Pay->save();
            } elseif ($type == 2) { //订单支付
                $Pay->transaction_id = $data['trade_no'];
                $Pay->out_trade_no = $data['out_trade_no'];
                $Pay->order_id = $orderinfo->id;
                if ($orderinfo->order_type == 1) {  //商品类型(1:课程,2:商品,3:图文质询,4:电话预约,5:打赏)
                    $Pay->order_type = 1; //订单类型(1:课程,2:商品,3:质询,4商圈,5:打赏)
                } elseif ($orderinfo->order_type == 2) {
                    $Pay->order_type = 2;
                } elseif ($orderinfo->order_type == 3 || $orderinfo->order_type == 4) {
                    $Pay->order_type = 3;
                } elseif ($orderinfo->order_type == 5) {
                    $Pay->order_type = 5;
                }
                $Pay->pay_type = 1;
                $Pay->make_type = 1;
                $Pay->user_id = $orderinfo->user_id;
                $Pay->work_id = $orderinfo->work_id;
                $Pay->money = $data['total_amount'];//$orderinfo->total_price;
                return $Pay->save();
            }
        }
    }







}
